package edu.rpi.semantic.utils;

import java.util.Calendar;

public class Log {
	public static int last_h = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
	public static int last_m = Calendar.getInstance().get(Calendar.MINUTE);
	public static int last_s = Calendar.getInstance().get(Calendar.SECOND);

	/**
	 * add "0" infront of 0-9
	 * @param digital
	 * @return
	 */
	private static String getDoubleDigital(int digital) {
		if (digital>9)
			return Integer.toString(digital);
		else
			return "0" + Integer.toString(digital);
	}
	
	/**
	 * err log
	 * @param err
	 */
	public static void err(String err) {
		int h = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); 
		int m = Calendar.getInstance().get(Calendar.MINUTE); 
		int s = Calendar.getInstance().get(Calendar.SECOND); 

		if (s<last_s) {
			if (m-1<last_m)
				System.err.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-1-last_h) + ":" + getDoubleDigital(m+59-last_m) + ":" + getDoubleDigital(s+60-last_s) + "): "+err);
			else
				System.err.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-last_h) + ":" + getDoubleDigital(m-1-last_m) + ":" + getDoubleDigital(s+60-last_s) + "): "+err);
		} else {
			if (m<last_m)
				System.err.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-1-last_h) + ":" + getDoubleDigital(m+60-last_m) + ":" + getDoubleDigital(s-last_s) + "): "+err);
			else
				System.err.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-last_h) + ":" + getDoubleDigital(m-last_m) + ":" + getDoubleDigital(s-last_s) + "): "+err);
		}
		
		last_h = h;
		last_m = m;
		last_s = s;
	}
	
	/**
	 * normal log
	 * @param msg
	 */
	public static void out(String msg) {
		int h = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); 
		int m = Calendar.getInstance().get(Calendar.MINUTE); 
		int s = Calendar.getInstance().get(Calendar.SECOND); 

		if (s<last_s) {
			if (m-1<last_m)
				System.out.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-1-last_h) + ":" + getDoubleDigital(m+59-last_m) + ":" + getDoubleDigital(s+60-last_s) + "): "+msg);
			else
				System.out.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-last_h) + ":" + getDoubleDigital(m-1-last_m) + ":" + getDoubleDigital(s+60-last_s) + "): "+msg);
		} else {
			if (m<last_m)
				System.out.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-1-last_h) + ":" + getDoubleDigital(m+60-last_m) + ":" + getDoubleDigital(s-last_s) + "): "+msg);
			else
				System.out.println(getDoubleDigital(h)+":"+getDoubleDigital(m)+":"+getDoubleDigital(s)+": (" + getDoubleDigital(h-last_h) + ":" + getDoubleDigital(m-last_m) + ":" + getDoubleDigital(s-last_s) + "): "+msg);
		}

		last_h = h;
		last_m = m;
		last_s = s;
	}
}
